package com.alex.guava;

import com.google.common.collect.MapDifference;
import com.google.common.collect.Maps;
import org.junit.Assert;
import org.junit.Test;

import java.util.AbstractMap;
import java.util.Collections;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Author  : gaojun            <br/>
 * Date    : 2020/7/29                          <br/>
 * Time    : 11:10 上午                         <br/>
 * ---------------------------------------    <br/>
 * Desc    :
 */
public class MapDiff {

    @Test
    public void testEq() {
        MapDifference difference = Maps.difference(Collections.unmodifiableMap(Stream.of(
                new AbstractMap.SimpleEntry<>("0","v0"),
                new AbstractMap.SimpleEntry<>("1","v1")).collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()))),
                Collections.unmodifiableMap(Stream.of(
                        new AbstractMap.SimpleEntry<>("0","v0"),
                        new AbstractMap.SimpleEntry<>("1","v1")).collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()))));
        Assert.assertTrue(difference.areEqual());
    }

    @Test
    public void testNotEqVal() {
        MapDifference difference = Maps.difference(Collections.unmodifiableMap(Stream.of(
                new AbstractMap.SimpleEntry<>("0","v0"),
                new AbstractMap.SimpleEntry<>("1","v1_1")).collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()))),
                Collections.unmodifiableMap(Stream.of(
                        new AbstractMap.SimpleEntry<>("0","v0"),
                        new AbstractMap.SimpleEntry<>("1","v1")).collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()))));
        Assert.assertFalse(difference.areEqual());
    }

    @Test
    public void testNotEqNoKey() {
        MapDifference difference = Maps.difference(Collections.unmodifiableMap(Stream.of(
                new AbstractMap.SimpleEntry<>("0","v0"),
                new AbstractMap.SimpleEntry<>("1","v1"),
                new AbstractMap.SimpleEntry<>("2","v2")).collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()))),
                Collections.unmodifiableMap(Stream.of(
                        new AbstractMap.SimpleEntry<>("0","v0"),
                        new AbstractMap.SimpleEntry<>("1","v1")).collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()))));
        Assert.assertFalse(difference.entriesOnlyOnLeft().isEmpty());
        difference.entriesOnlyOnLeft().forEach((k,v) -> System.out.println(k + ":" + v));
    }

    @Test
    public void testValDiffNotEmpty() {
        MapDifference difference = Maps.difference(Collections.unmodifiableMap(Stream.of(
                new AbstractMap.SimpleEntry<>("0","v0"),
                new AbstractMap.SimpleEntry<>("1","v1_1")).collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()))),
                Collections.unmodifiableMap(Stream.of(
                        new AbstractMap.SimpleEntry<>("0","v0"),
                        new AbstractMap.SimpleEntry<>("1","v1")).collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()))));
        Assert.assertFalse(difference.entriesDiffering().isEmpty());
        difference.entriesDiffering().forEach((k,v) -> System.out.println(k + ":" + v));
    }

    @Test
    public void testValDiffEmptyAndNoKey() {
        MapDifference difference = Maps.difference(Collections.unmodifiableMap(Stream.of(
                new AbstractMap.SimpleEntry<>("0","v0"),
                new AbstractMap.SimpleEntry<>("1","v1_1"),
                new AbstractMap.SimpleEntry<>("2","v2")).collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()))),
                Collections.unmodifiableMap(Stream.of(
                        new AbstractMap.SimpleEntry<>("0","v0"),
                        new AbstractMap.SimpleEntry<>("1","v1")).collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()))));
        Assert.assertFalse(difference.entriesOnlyOnLeft().isEmpty());
        Assert.assertFalse(difference.entriesDiffering().isEmpty());
    }
}
